﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Factorial
{
    static class Factorial
    {
        /// <summary>
        /// Returns number!
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        /// <exception cref="OverflowException"></exception>
        public static long Get(int number)
        {
            long result = 1;
            try
            {
                Handle(number, ref result);
            }
            catch (OverflowException ex)
            {
                
                throw ex;
            }
            return result;
        }

        private static void Handle(int number,ref long result)
        {
            if (number > 1)
            {
                try
                {
                    checked
                    {
                        result = (number--) * result;
                    }
                }
                catch (OverflowException ex)
                {
                    
                    throw ex;
                }
                
                Handle(number, ref result);
            }
        }
    }
}
